package com.example.lovedogs.zizhen;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Random;

import org.json.JSONArray;

import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import com.example.lovedogs.R;
import com.example.lovedogs.R.id;
import com.example.lovedogs.R.layout;
import com.example.lovedogs.R.string;
import com.example.lovedogs.domain.SickInfoDisease;
import com.example.lovedogs.domain.SickInfoFast;
import com.example.lovedogs.domain.parent.SickInfo;
import com.example.lovedogs.ui.KeywordsFlow;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class SickInfofastActivity extends Activity {
	private KeywordsFlow kf;
	private String url;
//	String[] keywords=new String[]{"123","qqq"};
	String[] keywords;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sickinfofast1);
		kf = (KeywordsFlow) findViewById(R.id.kf_sickinfofast);

		/*
		 * 获取数据
		 */
		achieveKeywords();

		kf.setDuration(800l);
		kf.setOnItemClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				 String keyword = ((TextView) v).getText().toString();  
			     Intent intent=new Intent(SickInfofastActivity.this, SickInfoDiseaseActivity.class);
			     intent.putExtra("keyword", keyword);
			     startActivity(intent);
			     

			}

		});
//		feedKeywordsFlow(kf, keywords);
//		kf.go2Show(KeywordsFlow.ANIMATION_IN);
	}

	private String[] achieveKeywords() {
		String[] sdata;
		url = getString(R.string.serverurl) + "JsonSickInfoFastServlet";
		HttpUtils http = new HttpUtils();
		http.send(HttpMethod.POST, url, new RequestCallBack<String>() {
			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				Gson g = new Gson();
				Type type=new TypeToken<List<String>>(){}.getType();
				List<String> data=g.fromJson(responseInfo.result, type);
				
				int size =data.size();
				keywords = (String[])data.toArray(new String[size]);
				feedKeywordsFlow(kf, keywords);
				kf.go2Show(KeywordsFlow.ANIMATION_OUT);
			
			}

			@Override
			public void onFailure(HttpException error, String msg) {
				Toast.makeText(SickInfofastActivity.this,
						"onErrorResponse" + error.getMessage(),
						Toast.LENGTH_LONG).show();

			}
		});

		return null;
	}

	private static void feedKeywordsFlow(KeywordsFlow keywordsFlow, String[] arr) {
		
		for (int i = 0; i <arr.length; i++) {
			keywordsFlow.feedKeyword(arr[i]);
		}
	}

}
